﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
namespace BLL
{
    public class BuyCart
    {
        //新增购物车,创建一个用户的时候为其生成一个购物车
        public int AddBuyCart(string account)
        {
            BuyCartInfo buycart = new BuyCartInfo(account);
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            return BuyCart.InsertBuyCart(buycart);
        }
        //删除购物车
        public int DelBuyCart(string account)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            return BuyCart.DeleteBuyCart(account);
        }
        //获取用户的购物车信息
        public BuyCartInfo GetBuyCart(string account)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            return BuyCart.GetBuyCart(account);
        }
        //商品页面加入购物车
        public int AddOneBuyCart(string account,string pid ,int num)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            BuyCartInfo buyCart = BuyCart.GetBuyCart(account);
            if (buyCart.PidList == null)
            {
                //buyCart.PidList = new List<string>();
                buyCart.PidList.Add(pid);
               // buyCart.Bnum = new List<int>();
                buyCart.Bnum.Add(num);
            }
            else
            {
                int index = buyCart.PidList.IndexOf(pid);//获取当前商品的pid在PidList的下标
                if (index == -1)//如果该商品并不在购物车中
                {
                    buyCart.PidList.Add(pid);//添加该商品
                    buyCart.Bnum.Add(num);
                }
                else
                {//如果该商品已经在购物车中
                    buyCart.Bnum[index] += num;//只需要加上num
                }
            }
            return BuyCart.UpdateBuyCart(buyCart,account);
        }

        //购物车页面减去/删除购物车商品/购买完成需要从购物车里减去已购买的商品
        public int DelOneBuyCart(string account,string pid)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            BuyCartInfo buyCart = BuyCart.GetBuyCart(account);
            int index = buyCart.PidList.IndexOf(pid);           
            buyCart.PidList.Remove(pid);
            buyCart.Bnum.RemoveAt(index);                      
            return BuyCart.UpdateBuyCart(buyCart, account);
        }

        public int SubOneBuyCart(string account, string pid, int num)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            BuyCartInfo buyCart = BuyCart.GetBuyCart(account);
            int index = buyCart.PidList.IndexOf(pid);
            if (buyCart.Bnum[index] - num <= 0)
            {
                buyCart.PidList.Remove(pid);
                buyCart.Bnum.RemoveAt(index);
            }
            else
            {
                buyCart.Bnum[index] -= num;
            }
            return BuyCart.UpdateBuyCart(buyCart, account);
        }

        //购物车页面修改数量
        public int UpdOneBuyCart(string account,string pid,int num)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            BuyCartInfo buyCart = BuyCart.GetBuyCart(account);
            int index = buyCart.PidList.IndexOf(pid);
            buyCart.Bnum[index] = num;
            return BuyCart.UpdateBuyCart(buyCart, account);
        }

        public int UpdBuyCartAct(string newact, string oldact)
        {
            DAL.BuyCart BuyCart = new DAL.BuyCart();
            return BuyCart.UpdBuyAct(newact, oldact);
        }
    }
}
